package com.qf.service.impl;

import com.qf.dao.HouseInfoDao;
import com.qf.dao.MailVerificationDao;
import com.qf.dao.UserInfoDao;
import com.qf.pojo.HouseInfo;
import com.qf.pojo.MailVerification;
import com.qf.pojo.UserInfo;
import com.qf.pojo.resp.BaseResult;
import com.qf.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.mail.javamail.JavaMailSender;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    BaseResult baseResult = new BaseResult();
    @Resource
    UserInfoDao userInfoDao;
    @Resource
    HouseInfoDao houseInfoDao;
    @Resource
    MailVerificationDao mailVerificationDao;
    @Autowired
    JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String from;

    @Override
    public BaseResult login(UserInfo userInfo, HttpSession session) {
        //System.out.println("请求登录用户："+userInfo);
        UserInfo userInfo1 = userInfoDao.findUserByTel(userInfo.getTel());
        if (userInfo1!=null) {
            if (userInfo1.getPassword().equals(userInfo.getPassword())) {
                session.setAttribute("user", userInfo1);

                System.out.println(houseInfoDao.findHouseByUid(userInfo1.getId()));

                if(houseInfoDao.findHouseByUid(userInfo1.getId()).isEmpty()){
                    baseResult.setCode(1);
                    baseResult.setInfo("登录成功");
                    baseResult.setData(userInfo1);
                }else{
                    baseResult.setCode(2);
                    baseResult.setInfo("登录成功");
                    baseResult.setData(userInfo1);
                }
                return baseResult;
            } else {
                baseResult.setCode(0);
                baseResult.setInfo("输入的密码有误，请确认后输入！");
                return baseResult;
            }
        }else {
            baseResult.setCode(0);
            baseResult.setInfo("输入的用户名有误，请重新输入！");
            return baseResult;
        }
    }

    @Override
    public BaseResult addUser(UserInfo userInfo) {
        UserInfo userName = userInfoDao.findUserByTel(userInfo.getTel());
        if (userName!=null){
            baseResult.setCode(2);
            baseResult.setInfo("用户名已存在，请更换");
            return baseResult;
        }else{
            //增加到数据库  将请求对象转为数据库存储对象
            //UserInfo user = new UserInfo();
            //BeanUtils.copyProperties(userInfo,user);
            //user.setCreat_time(new Date());

            userInfoDao.addUser(userInfo);
            baseResult.setCode(1);
            baseResult.setInfo("注册成功");
            return baseResult;
        }


    }

    @Override
    public BaseResult updateUser(UserInfo userInfo,HttpSession session) {
        UserInfo userInfo1 =(UserInfo) session.getAttribute("user");
        if(userInfo.getPassword().equals(userInfo1.getPassword())){
            baseResult.setCode(2);
            userInfoDao.updateUser(userInfo);
            baseResult.setInfo("成功");
            return baseResult;
        }else{
            baseResult.setCode(1);
            userInfoDao.updateUser(userInfo);
            baseResult.setInfo("成功");
            return baseResult;
        }
    }

    @Override
    public BaseResult deleteUserById(Integer id) {
        userInfoDao.deleteUserById(id);
        baseResult.setCode(1);
        baseResult.setInfo("成功");
        return baseResult;
    }

    @Override
    public BaseResult findAllUser() {
        List<UserInfo> list = userInfoDao.findAllUser();
        baseResult.setCode(1);
        baseResult.setInfo("成功");
        baseResult.setData(list);
        return baseResult;
    }

    @Override
    public BaseResult logout(HttpSession session) {
        session.removeAttribute("user");
        baseResult.setCode(1);
        baseResult.setInfo("成功");
        return baseResult;
    }

    @Override
    public UserInfo findUserByTel(String tel) {
        return userInfoDao.findUserByTel(tel);
    }

    @Override
    public BaseResult findOneBySession(HttpSession session) {
        UserInfo userInfo =(UserInfo) session.getAttribute("user");
        baseResult.setCode(1);
        baseResult.setInfo("成功");
        baseResult.setData(userInfo);
        return baseResult;
    }

//    @Override
//    public BaseResult sendMail(String username) {
//        System.out.println("邮件发送地址："+userName);
//        TbUser byUserName = tbUserDao.findByUserName(userName);
//        if (byUserName==null){
//            //生成一个随机验证码
//            Random random = new Random();
//            StringBuffer stringBuffer = new StringBuffer();
//            for (int i = 0; i <4 ; i++) {
//                stringBuffer.append(random.nextInt(10));
//            }
//
//            try {
//                //邮件发送
//                SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
//                simpleMailMessage.setTo(userName);
//                simpleMailMessage.setFrom(from);
//                simpleMailMessage.setSubject("注册验证码");
//                simpleMailMessage.setText(stringBuffer.toString());
//                javaMailSender.send(simpleMailMessage);
//
//                //如果发送成功  则存入数据库
//                MailVerification mailVerification = new MailVerification();
//                mailVerification.setCode(stringBuffer.toString());
//                mailVerification.setStatus(0);
//                mailVerification.setCreatTime(new Date());
//                mailVerification.setUserName(userName);
//
//                Integer i = mailVerificationDao.insertMail(mailVerification);
//                baseResult.setCode(1);
//                baseResult.setInfo("邮件发送成功！");
//                return baseResult;
//            }catch (Exception e){
//                e.printStackTrace();
//                baseResult.setCode(0);
//                baseResult.setInfo("邮件发送失败！");
//                return baseResult;
//            }
//
//        }
//        baseResult.setCode(0);
//        baseResult.setInfo("用户名已存在，请登录");
//        return baseResult;
//    }


}
